package com.easy.system.dao.impl;

import com.easy.system.dao.HibernateBaseDao;
import com.easy.system.util.Condition;
import com.easy.system.util.Pagination;
import com.easy.yours.biz.util.SqlUtil;
import org.hibernate.Query;
import org.hibernate.type.Type;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sunguangzhu
 * Date: 13-6-18
 * Time: 下午1:20
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class HibernateBaseDaoImpl<T> extends HibernateTemplate implements HibernateBaseDao<T> {
    @Override
    public List<T> find(Condition condition, final Pagination pagination) {
        Map<String, Object> map = SqlUtil.formatSql(null,condition);
        final StringBuilder hql = (StringBuilder) map.get("hql");
        final Object[] values = (Object[]) map.get("values");
        final Type[] types = (Type[]) map.get("types");
        Query query = this.getSession().createQuery(hql.toString());
        query.setParameters(values, types);
        if (pagination != null) {
            query.setFirstResult(pagination.getFirstTotal());
            query.setMaxResults(pagination.getPageSize());
        }
        return query.list();
    }

    //To change body of implemented methods use File | Settings | File Templates.
}
